package com.example.erp.bus.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.erp.bus.entity.Goods;
import com.example.erp.bus.entity.GoodsType;
import com.example.erp.bus.entity.Supplier;
import com.example.erp.bus.service.GoodsService;
import com.example.erp.bus.service.GoodsTypeService;
import com.example.erp.bus.service.SupplierService;
import com.example.erp.bus.vo.GoodsVo;
import com.example.erp.bus.vo.GoodstypeVo;
import com.example.erp.common.utils.DataGridViewResult;
import com.example.erp.common.utils.JSONResult;
import com.example.erp.common.utils.SystemConstant;
import com.example.erp.common.utils.TreeNode;
import com.example.erp.sys.entity.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author LiQiCheng
 * @since 2022-02-19
 */
@RestController
@RequestMapping("/bus/goods")
public class GoodsController {

    @Resource
    private GoodsTypeService goodsTypeService;
    @Resource
    private GoodsService goodsService;
    @Resource
    private SupplierService supplierService;

    @RequestMapping("/loadGoodsTreeLeft")
    public DataGridViewResult loadGoodsTreeLeft() {
        //查询所有分类
        List<GoodsType> goodstypeList = goodsTypeService.list();
        //创建节点集合
        List<TreeNode> treeNodes = new ArrayList<>();
        //循环遍历所有分类
        for (GoodsType goodstype : goodstypeList) {
            //如果状态为1，节点为展开状态
            Boolean spread = goodstype.getOpen() == SystemConstant.OPEN_TRUE ? true : false;
            //封装树节点
            TreeNode treeNode = new TreeNode();
            treeNode.setId(goodstype.getId());//分类编号
            treeNode.setPid(goodstype.getPid());//父级分类编号
            treeNode.setTitle(goodstype.getTitle());//分类名称
            treeNode.setSpread(spread);//展开状态
            //将树节点对象添加到节点集合
            treeNodes.add(treeNode);
        }
        return new DataGridViewResult(treeNodes);
    }

    /**
     * 商品列表
     *
     * @param goodsVo
     * @return
     */
    @RequestMapping("/goodslist")
    public DataGridViewResult goodslist(GoodsVo goodsVo) {
        //创建分页对象
        IPage<Goods> page = new Page<Goods>(goodsVo.getPage(), goodsVo.getLimit());
        IPage<Goods> goodsIPage = goodsService.findGoodsListByPage(page, goodsVo);
        //返回数据
        return new DataGridViewResult(goodsIPage.getTotal(), goodsIPage.getRecords());
    }

    @RequestMapping("/allSupplier")
    public List<Supplier> allSupplier() {
        return supplierService.list();
    }

    /**
     * 添加商品
     *
     * @param goods
     * @return
     */
    @RequestMapping("/addGoods")
    public JSONResult addGoods(Goods goods) {
        if (goodsService.save(goods)) {
            return SystemConstant.ADD_SUCCESS;
        }
        return SystemConstant.ADD_ERROR;
    }

    /**
     * 修改商品
     *
     * @param goods
     * @return
     */
    @RequestMapping("/updateGoods")
    public JSONResult updateGoods(Goods goods) {
        if (goodsService.updateById(goods)) {
            return SystemConstant.UPDATE_SUCCESS;
        }
        return SystemConstant.UPDATE_ERROR;
    }

    /**
     * 删除商品
     *
     * @param id
     * @return
     */
    @RequestMapping("/deleteById")
    public JSONResult deleteById(int id) {
        if (goodsService.removeById(id)) {
            return SystemConstant.DELETE_SUCCESS;
        } else {
            return SystemConstant.DELETE_ERROR;
        }
    }
}

